package main

import (
	"fmt"
	"go_code/chatroom/server/model"
	"net"
	"time"
)

func main() {
	initRedisPool("localhost:6379", 8, 0, 100 * time.Second)
	model.UserDaoObj = model.NewUserDao(redisPool)

	listener, err := net.Listen("tcp", "0.0.0.0:8889")
	if err != nil {
		fmt.Println("net.Listen 返回 listener 失败", err)
		return
	}

	defer func() {
		err = listener.Close()
		if err != nil {
			fmt.Println("关闭 net listener 错误", err)
			return
		}
	}()

	for {
		conn, err := listener.Accept()
		if err != nil {
			fmt.Println("listener.Accept 建立连接错误", err)
		}

		go processing(conn)
	}
}

func processing(conn net.Conn) {
	defer func() {
		err := conn.Close()
		if err != nil {
			return 
		}
	}()

	processor := &Processor{
		Conn: conn,
	}
	err := processor.handle()
	if err != nil {
		return
	}
}